import { CommunityItem } from '@/types';

// 模拟延迟
const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

// 生成模拟小区数据
const generateMockData = (): CommunityItem[] => {
  const cities = ['北京', '上海', '广州', '深圳', '杭州', '成都'];
  const communities = [
    '碧桂园',
    '万科城',
    '保利心语',
    '融创壹号院',
    '绿地中央广场',
    '恒大翡翠华庭',
    '龙湖天街',
    '中海国际社区',
    '华润悦府',
    '招商雍景湾',
    '金地格林小镇',
    '富力桃园',
  ];

  const descriptions = [
    '距离地铁站500米，周边配套齐全，生活便利',
    '靠近商业中心，交通便利，周边有多所学校',
    '环境优美，绿化率高，适合居住',
    '高端社区，安保严格，物业服务优质',
    '临近公园，空气清新，适合家庭居住',
    '交通便利，距离高速路口仅2公里',
    '周边医疗设施完善，生活配套齐全',
    '靠近大型商场，购物方便，餐饮丰富',
    '新建社区，设施现代化，停车位充足',
    '学区房，周边有重点小学和中学',
  ];

  const data: CommunityItem[] = [];

  for (let i = 1; i <= 50; i++) {
    const city = cities[Math.floor(Math.random() * cities.length)];
    const community =
      communities[Math.floor(Math.random() * communities.length)];
    const buildingCount = Math.floor(Math.random() * 15) + 3; // 3-17栋
    const unitCount = Math.floor(Math.random() * 4) + 1; // 1-4单元
    const floorCount = Math.floor(Math.random() * 20) + 6; // 6-25层
    const householdPerFloor = Math.floor(Math.random() * 4) + 2; // 2-5户

    data.push({
      id: `community-${i.toString().padStart(3, '0')}`,
      cityName: city,
      communityName: `${community}${i > 8 ? i + '期' : ''}`,
      address: `${city}市天河区${community}路${
        Math.floor(Math.random() * 200) + 1
      }号`,
      houseCount: Math.floor(Math.random() * 100), // 0-99套房屋
      buildingInfo: `${buildingCount}栋${unitCount}单元${floorCount}层${householdPerFloor}户`,
      desc: descriptions[Math.floor(Math.random() * descriptions.length)],
    });
  }

  return data;
};

let mockCommunityData = generateMockData();

export default {
  // 获取小区列表
  'GET /api/community/list': async (req: any, res: any) => {
    await waitTime(500);

    const { current = 1, pageSize = 10, cityName, communityName } = req.query;

    let filteredData = [...mockCommunityData];

    // 城市筛选
    if (cityName) {
      filteredData = filteredData.filter((item) =>
        item.cityName.includes(cityName),
      );
    }

    // 小区名称筛选
    if (communityName) {
      filteredData = filteredData.filter((item) =>
        item.communityName.includes(communityName),
      );
    }

    // 分页
    const start = (current - 1) * pageSize;
    const end = start + parseInt(pageSize);
    const paginatedData = filteredData.slice(start, end);

    res.json({
      success: true,
      data: paginatedData,
      total: filteredData.length,
    });
  },

  // 更新小区描述
  'POST /api/community/updateDesc': async (req: any, res: any) => {
    await waitTime(300);
    const { id, desc } = req.body;

    const community = mockCommunityData.find((item) => item.id === id);
    if (community) {
      community.desc = desc;
      res.json({
        success: true,
        message: '更新成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '小区不存在',
      });
    }
  },

  // 删除小区
  'POST /api/community/delete': async (req: any, res: any) => {
    await waitTime(300);
    const { id } = req.body;

    const index = mockCommunityData.findIndex((item) => item.id === id);
    if (index !== -1) {
      mockCommunityData.splice(index, 1);
      res.json({
        success: true,
        message: '删除成功',
      });
    } else {
      res.status(404).json({
        success: false,
        message: '小区不存在',
      });
    }
  },
};
